<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
    <title>待收快递</title>
    <jsp:include page="../../views/layout/inc.jsp"></jsp:include>
    <link rel="stylesheet" href="/css/core/easyui/easyui-rtl.css?v=201505241044"/>
    <style type="text/css">
        .tdh {
            font-weight: bold;
            width: 80px;
            text-align: right;
            padding-right: 10px;
        }

        .txtInput, .queryInput {
            width: 200px;
        }

        .queryInput {
            height: 30px;
            font-size: 25px;
            color: red;
        }

    </style>
    <%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/editPage')}"> --%>
    <script type="text/javascript">
        $.canEdit = true;
    </script>
    <%-- </c:if> --%>
    <%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/delete')}"> --%>
    <script type="text/javascript">
        $.canDelete = true;
    </script>
    <%-- </c:if> --%>
    <script type="text/javascript">

        var dataGrid;
        $(function () {
            dataGrid = $('#dataGrid').datagrid({
                singleSelect: true,
                url: '/recevieCourier/dataGrid',
                fit: true,
                fitColumns: false,
                border: false,
                pagination: true,
                idField: 'id',
                pageSize: 15,
                pageList: [15, 30, 50, 100],
                sortName: 'gmtModify',
                sortOrder: 'desc',
                checkOnSelect: true,
                selectOnCheck: true,
                nowrap: true,
                queryParams: {
                    status: 1
                },
                columns: [[{
                    checkbox: true,
                    field: 'id',
                    title: 'ID',
                }, {
                    field: 'outBizNum',
                    title: '快递单号',
                    width: 100
                }, {
                    field: 'status',
                    title: '状态',
                    width: 100,
                    formatter: function (val, rec) {
                        if (val == 1) {
                            return '待取';
                        } else if (val == 2) {
                            return '<span style="color:green">已取件</span>';
                        } else if (val == 3) {
                            return '<span style="color:red">关闭</span>';
                        } else if (val == 4) {
                            return '删除';
                        } else {
                            return '未知';
                        }
                    }
                }, {
                    field: 'name',
                    title: '订单名称',
                    width: 100
                }, {
                    field: 'companyName',
                    title: '快递公司',
                    width: 200
                }, {
                    field: 'desAccountName',
                    title: '收件人',
                    width: 100
                }, {
                    field: 'desMobile',
                    title: '收件人电话',
                    width: 100
                }, {
                    field: 'remark',
                    title: '快递提取码',
                    width: 100
                }, {
                    field: 'gmtCreate',
                    title: '收件时间',
                    width: 135,
                    sortable: true,
                    formatter: function (val, rec) {
                        return new Date(val).format("yyyy-MM-dd hh:mm:ss");
                    }
                }, {
                    field: 'gmtModify',
                    title: '取件时间',
                    width: 135,
                    sortable: true,
                    formatter: function (val, rec) {
                        if (val == "" || val == null) {
                            return "";
                        }
                        return new Date(val).format("yyyy-MM-dd hh:mm:ss");
                    }
                }]],

                toolbar: '#toolbar',
                onLoadSuccess: function () {
                    $('#searchForm table').show();
                    parent.$.messager.progress('close');

                    $(this).datagrid('tooltip');
                },
                onRowContextMenu: function (e, rowIndex, rowData) {
                    e.preventDefault();
                    $(this).datagrid('unselectAll').datagrid('uncheckAll');
                    $(this).datagrid('selectRow', rowIndex);
                },
                onDblClickRow: function (idx, item) {
                    editFun(item);
                }
            });

            $.ajax({
                type: 'post',
                url: '/companyExpressController/getCombobox',
                success: function (result) {
                    $('#companyId').combobox({
                        data: result,
                        panelHeight: 'auto',
                        editable: false
                    });
                    sessionStorage.setItem("comboboxExpress", JSON.stringify(result));
                },
                dataType: 'json'
            });


            $('#status').combobox({
                data: [{"value": 1, "text": "待取件"},
                    {"value": 2, "text": "已取件"},
                    {"value": 3, "text": "关闭"}],
                panelHeight: 'auto',
                editable: false
            });

            document.onkeydown = function (e) {
                var ev = window.event || e;
                if (ev.keyCode == 13) {
                    searchFun();
                }
            }

            <c:if test="${not empty param}">
            dataGrid.datagrid('load', {"status": "${param.status}"});
            </c:if>


            // Accordion
            $('#queryAccordion').accordion({
                onUnselect: function (title, idx) {
                    if (0 == idx) {
                        $('#queryAccordion').accordion("select", 1);
                        $('#expressNumber').focus();
                        $('#expressNumber').val("");
                    } else {
                        $('#queryAccordion').accordion("select", 0);
                        $('#remark').focus();

                    }
                }
            });

            // 出库
            function warehouseOut() {
                var val = $("#expressNumber").val();
                if (val && val.length > 10) {
                    console.log(val);
                    $.post('/recevieCourier/warehouseOut', {
                        expressNumber: val
                    }, function (result) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        if (result.success) {
                            dataGrid.datagrid('reload');
                        }
                        parent.$.messager.progress('close');
                    }, 'JSON');

                }
            }

            $("#expressNumber").on('input propertychange', warehouseOut);
            $("#btnWarehouseOut").click(warehouseOut);
        });


        function addFun() {
            parent.$.modalDialog({
                title: '新增待取快递单',
                width: 600,
                height: 350,
                href: '/recevieCourier/addPage',
                buttons: [{
                    text: '确认新增',
                    handler: function () {
                        parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                        var f = parent.$.modalDialog.handler.find('#form');
                        f.submit();
                    }
                }]
            });
        }

        function notice() {
            var row = $("#dataGrid").datagrid("getSelected");
            if (row) {
                if (row.status == 2) {
                    parent.$.messager.alert('提示', '该记录已经取件!', 'info');
                    return false;
                }
                if (row.status == 3) {
                    parent.$.messager.alert('提示', '该记录已经关闭!', 'info');
                    return false;
                }
                var url = '/recevieCourier/notice';
                $.post(url, {'id': row.id}, function (data) {
                    $.messager.alert("提示", data.msg, "info");
                }, 'json');
            } else {
                $.messager.alert("提示", "请选择一条记录", "info");
            }
        }

        function searchFun() {
            dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
        }
        function cleanFun() {
            $('#searchForm input').val('');
            dataGrid.datagrid('load', {});
        }

        function editFun(item) {
            item = item || $("#dataGrid").datagrid("getSelected");
            if (!item) {
                parent.$.messager.alert('提示', '请选择快递数据!', 'info');
                return false;
            }

            if (2 == item.status) {
                parent.$.messager.alert('提示', '该记录已经取件!', 'info');
                return false;
            } else if (3 == item.status) {
                parent.$.messager.alert('提示', '该记录已经关闭!', 'info');
                return false;
            }

            parent.$.modalDialog({
                title: '修改待取快递单',
                width: 600,
                height: 380,
                href: '/recevieCourier/editPage?id=' + item.id,
                buttons: [{
                    text: '确认修改',
                    handler: function () {
                        parent.$.modalDialog.openner_dataGrid = dataGrid;//因为修改成功之后，需要刷新这个treeGrid，所以先预定义好
                        var f = parent.$.modalDialog.handler.find('#form');
                        f.submit();
                    }
                }]
            });
        }

        function signFun(item) {
            item = item || $("#dataGrid").datagrid("getSelected");
            if (!item) {
                parent.$.messager.alert('提示', '请选择快递数据!', 'info');
                return false;
            }

            if (2 == item.status) {
                parent.$.messager.alert('提示', '该记录已经取件!', 'info');
                return false;
            } else if (3 == item.status) {
                parent.$.messager.alert('提示', '该记录已经关闭!', 'info');
                return false;
            }

            parent.$.messager.confirm('询问', '确认快递签收？', function (b) {
                if (b) {
                    parent.$.messager.progress({
                        title: '提示',
                        text: '数据处理中，请稍后....'
                    });
                    $.post('/recevieCourier/warehouseOut', {
                        id: item.id
                    }, function (result) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        if (result.success) {
                            dataGrid.datagrid('reload');
                        }
                        parent.$.messager.progress('close');
                    }, 'JSON');
                }
            });
        }

        function removeFun(item) {
            item = item || $("#dataGrid").datagrid("getSelected");
            if (!item) {
                parent.$.messager.alert('提示', '请选择快递数据!', 'info');
                return false;
            }

            parent.$.messager.confirm('询问', '您是否要删除当前待取快递？', function (b) {
                if (b) {
                    parent.$.messager.progress({
                        title: '提示',
                        text: '数据处理中，请稍后....'
                    });
                    $.post('/recevieCourier/delete', {
                        id: item.id
                    }, function (result) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        if (result.success) {
                            dataGrid.datagrid('reload');
                        }
                        parent.$.messager.progress('close');
                    }, 'JSON');
                }
            });
        }

        function downloadExcel() {
            var url = '/recevieCourier/downloadExcel';
            var queryData = $.serializeObject($('#searchForm'));
            var index = 0;
            for (var name in queryData) {
                if (index == 0) {
                    url += "?";
                } else {
                    url += "&";
                }
                url += name + "=" + queryData[name];
                index++;
            }
            window.open(url);
        }
    </script>
</head>
<body>


<div class="easyui-layout" data-options="fit:true, border:true">
    <div id="queryAccordion" class="easyui-accordion" data-options="region:'north',border:false" style="height:150px;">
        <div title="按条件查询" style="padding:10px; overflow: hidden">
            <form id="searchForm">
                <table class="table table-hover table-condensed">
                    <tr>
                        <td class="tdh"><label for="companyId">快递点</label></td>
                        <td><input name="companyId" id="companyId" class="txtInput"/></td>
                        <td class="tdh"><label for="status">快递状态</label></td>
                        <td><input name="status" id="status" class="txtInput"/></td>
                        <td class="tdh"><label for="desAccountName">收件人</label></td>
                        <td><input name="desAccountName" id="desAccountName" class="easyui-validatebox txtInput"/></td>
                    </tr>
                    <tr>
                        <td class="tdh" style="height: 50px"><label for="desMobile">收件人电话</label></td>
                        <td><input name="desMobile" id="desMobile" class="easyui-validatebox mobile queryInput"
                                   placeholder="电话号码模糊查询"/></td>
                        <td class="tdh"><label for="remark">快递提取码</label></td>
                        <td><input name="remark" id="remark" class="easyui-validatebox queryInput"
                                   placeholder="提取码模糊查询" autofocus /></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
            </form>
        </div>
        <div title="快速出库" style="padding:20px; vertical-align: middle">
            <span style="font-size: 22px; vertical-align: middle"><label for="expressNumber">快递出库:</label></span>&nbsp;
            <input class="queryInput" style="width: 300px" type="text" maxlength="15" id="expressNumber"
                   placeholder="条码枪扫描录入"/>
            <a href="javascript:void(0);" id="btnWarehouseOut" class="easyui-linkbutton"
               data-options="iconCls:'icon-search'"
               style="width:80px; height: 35px">出库</a>
        </div>
    </div>

    <div data-options="region:'center',border:false">
        <table id="dataGrid"></table>
    </div>
</div>
<div id="toolbar" style="display: none;">
    <a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton"
       data-options="plain:true,iconCls:'icon-add'">新增</a>

    <a onclick="notice();" href="javascript:void(0);" class="easyui-linkbutton"
       data-options="plain:true,iconCls:'icon-tip'">取件提醒</a>

    <a onclick="downloadExcel();" href="javascript:void(0);" class="easyui-linkbutton"
       data-options="plain:true,iconCls:'icon-redo'">导出</a>

    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true"
       onclick="searchFun();">提交查询</a>

    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true"
       onclick="cleanFun();">重置查询</a>

    <hr style="height:50px;width:2px; display: inline; margin: 0px 5px"/>

    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true"
       onclick="editFun();">修改</a>

    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-lock',plain:true"
       onclick="signFun();">出库</a>

    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true"
       onclick="removeFun();">删除</a>
</div>

<div id="menu" class="easyui-menu" style="width: 150px; display: none;">
    <%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/editPage')}"> --%>
    <div onclick="editFun();" data-options="iconCls:'icon-edit'">修改</div>
    <%-- 		</c:if> --%>
    <%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/addPage')}"> --%>
    <!-- <div onclick="addFun();" data-options="iconCls:'icon-add'">新增</div> -->
    <%-- 		</c:if> --%>
    <%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/delete')}"> --%>
    <div onclick="signedFun();" data-options="iconCls:'icon-edit'">出库</div>
    <div onclick="deleteFun();" data-options="iconCls:'icon-remove'">删除</div>
    <%-- 		</c:if> --%>
</div>
</body>
</html>